package ctr_layer;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.UUID;

import javax.xml.bind.annotation.adapters.HexBinaryAdapter;

import model_layer.StaffMember;
import db_layer.StaffMemberDB;

public class StaffMemberCtr
{
	private StaffMemberDB sm_db;
	
	public StaffMemberCtr()
	{
		sm_db = new StaffMemberDB();
	}
	
	public StaffMember find_staff_member(int id)
	{
		return sm_db.find_staff_member("id", id, true);
	}
	
	
	public List<StaffMember> find_deleted_staff_memebers()
	{
		return sm_db.find_particular_staff_members("deleted", 1, false);
	}
	
	public List<StaffMember> find_non_deleted_staff_members()
	{
		return sm_db.find_particular_staff_members("deleted", 0, false);
	}
	
	public StaffMember log_in_staff_member(String login, String password)
	{
		StaffMember sm = sm_db.find_staff_member("username", login, false);
		if(check_password(sm, password))
		{
			return sm;
		}
		return null;
	}
	
	public boolean username_exists(String login)
	{
		return sm_db.find_staff_member("username", login, false) != null;
	}
	
	public boolean insert_account(String fname, String lname, String phone_nr, String email, String login, String password, int privilage_level)
	{
		StaffMember sm = new StaffMember();
		sm.setDeleted(false);
		sm.setEmail(email);
		sm.setFname(fname);
		sm.setPrivilage_level(privilage_level);
		sm.setLname(lname);
		sm.setLogin(login);
		sm.setPhone_nr(phone_nr);
		sm.setPassword(hash_password(password, make_salt()));
		return sm_db.insert_staff_member(sm);
	}
	
	public boolean change_password(StaffMember sm, String password)
	{
		String password_hash = hash_password(password, make_salt());
		sm.setPassword(password_hash);
		return sm_db.update_staff_member(sm);
	}
	
	public boolean update_account(StaffMember sm, String fname, String lname, String phone_nr, String email, String login, int privilage_level)
	{
		sm.setFname(fname);
		sm.setLname(lname);
		sm.setEmail(email);
		sm.setPhone_nr(phone_nr);
		sm.setLogin(login);
		sm.setPrivilage_level(privilage_level);
		return sm_db.update_staff_member(sm);
	}
	
	public boolean delete_account(int id)
	{
		StaffMember sm = sm_db.find_staff_member("id", id, false);
		sm.setDeleted(true);
		return sm_db.update_staff_member(sm);
	}
	
	public boolean restore_account(int id)
	{
		StaffMember sm = sm_db.find_staff_member("id", id, false);
		sm.setDeleted(false);
		return sm_db.update_staff_member(sm);
	}
	
	private boolean check_password(StaffMember sm, String password)
	{
		if (sm == null)
		{
			return false;
		}
		String pass_hash = sm.getPassword();
		int salt_start = pass_hash.indexOf("|") + 1;
		String salt = pass_hash.substring(salt_start);
		return hash_password(password, salt).equals(pass_hash);
	}
	
	private String hash_password(String password, String salt)
	{
		String to_hash = password + salt;
		try
		{
			MessageDigest md =  MessageDigest.getInstance("SHA-256");
			String hex_hash = (new HexBinaryAdapter().marshal(md.digest(to_hash.getBytes())));
			hex_hash += "|" + salt;
			return hex_hash;
		}
		catch(NoSuchAlgorithmException e)
		{
			System.err.println("This algorithm doesn't exist: " + e);
			return null;
		}
	}
	
	private String make_salt()
	{	
		String rand = UUID.randomUUID().toString();
		return rand.substring(0, 5);
	}
	

}
